//
//  SignatrueViewController.swift
//  CHSimpleUI
//
//  Created by cyptt on 2022/7/25.
//

import UIKit
import OpenGLES
class SignatrueViewController: UIViewController {

    let context:EAGLContext? = EAGLContext.init(api: .openGLES2)
    override func viewDidLoad() {
        super.viewDidLoad()

     
        self.setNewOrientation(fullScreen: true)
        self.view.backgroundColor = UIColor.white
        
        self.view.addSubview(self.signatrueView)
        
        self.view.addSubview(self.btn)
        self.view.addSubview(self.erasBtn)
        self.view.addSubview(self.colorBtn)
        self.view.addSubview(self.widthBtn)
        
        self.view.addSubview(self.imageV)
        self.signatrueView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }

        self.btn.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(200)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        
        self.erasBtn.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(260)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        
        self.colorBtn.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(260 + 60 )
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        
        self.widthBtn.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(320 + 60)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        
        self.imageV.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(320 + 60 + 60)
            make.width.equalTo(200)
            make.height.equalTo(200)
        }
        
        self.btn.btnClickCallBack { btn in
         
            print("点击了")
            
            
            
            let image:UIImage? =   self.signatrueView.signatureImage
         
            self.imageV.image = image
            
  
            if image != nil{
                
                UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
            }
          
        }
        
        self.erasBtn.btnClickCallBack { btn in
            
            self.signatrueView.erase()
            
            
        }
        
        self.widthBtn.btnClickCallBack { btn in
            
        }
        
        self.colorBtn.btnClickCallBack { btn in
            
        }
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        
    }
    
    //横竖屏
     func setNewOrientation(fullScreen: Bool) {
         if fullScreen { //横屏
             let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
             UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
             
             let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.landscapeRight.rawValue)
             UIDevice.current.setValue(orientationTarget, forKey: "orientation")
             
         } else { //竖屏
             let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
             UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
             
             let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.portrait.rawValue)
             UIDevice.current.setValue(orientationTarget, forKey: "orientation")
         }
     }
    
    
    lazy var signatrueView:CHPPSSignatureView = {()-> CHPPSSignatureView in
        let v:CHPPSSignatureView = CHPPSSignatureView.init(frame: CGRect.zero, context: context!)
        v.backgroundColor = UIColor.red
        v.strokeColor = UIColor.black
   
        
        return v
    }()
    
    lazy var btn:CHBaseButton = {()-> CHBaseButton in
        let btn:CHBaseButton = CHBaseButton.init(type: UIButton.ButtonType.custom)
        btn.setTitle("截图", for: .normal)
        btn.backgroundColor = UIColor.red
        
        return btn
    }()
    
    lazy var erasBtn:CHBaseButton = {()-> CHBaseButton in
        let btn:CHBaseButton = CHBaseButton.init(type: UIButton.ButtonType.custom)
        btn.setTitle("擦", for: .normal)
        btn.backgroundColor = UIColor.red
        
        return btn
    }()
    lazy var colorBtn:CHBaseButton = {()-> CHBaseButton in
        let btn:CHBaseButton = CHBaseButton.init(type: UIButton.ButtonType.custom)
        btn.setTitle("颜色", for: .normal)
        btn.backgroundColor = UIColor.red
        
        return btn
    }()
    
    lazy var widthBtn:CHBaseButton = {()-> CHBaseButton in
        let btn:CHBaseButton = CHBaseButton.init(type: UIButton.ButtonType.custom)
        btn.setTitle("笔宽", for: .normal)
        btn.backgroundColor = UIColor.red
        
        return btn
    }()
    
    
    lazy var imageV:UIImageView = {()-> UIImageView in
        let imageV = UIImageView.init()
        imageV.backgroundColor = UIColor.gray
        return imageV
    }()

}
